package com.dao;

import java.util.List;
import com.hbt.HibernateUtil;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import com.bean.OrdenCompra;


public class OrdenCompraDAO {
	
	private static SessionFactory sf = null;
	private static OrdenCompraDAO instancia = null;
	
	public static OrdenCompraDAO getInstancia(){
		if(instancia == null){
			sf = HibernateUtil.getSessionFactory();
			instancia = new OrdenCompraDAO();
		}
		return instancia;
	}
	
	
	public void grabarOrdenCompra (OrdenCompra oc){
		Session session = sf.openSession();
		session.beginTransaction();
		session.persist(oc);
		session.flush();		
		session.getTransaction().commit();	
		session.close();
	}
	
	public OrdenCompra getOrdenCompra(OrdenCompra oc){
		Session session = sf.openSession();
		Transaction trans = session.beginTransaction();
		OrdenCompra orden = (OrdenCompra) session.get(OrdenCompra.class, oc.getId());
		trans.commit();
		session.close();
		return orden;
	}


	public List<OrdenCompra> getOrdenesCompra() {
		Session session = sf.openSession();
		List<OrdenCompra> resutl = (List<OrdenCompra>) session.createQuery("from OrdenCompra").list();
		session.close();
		return resutl;	
	}
}
